package com.atf.atfconfigurator.views;

import org.apache.commons.configuration.PropertiesConfiguration;

import com.atf.atfconfigurator.AtfconfiguratorUI;
import com.atf.atfconfigurator.composite.CollapseableBox;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class ClusterView  extends VerticalLayout implements View {
	private PropertiesConfiguration wasConfigProperty;
	@Override
	public void enter(ViewChangeEvent event) {
		// TODO Auto-generated method stub
		wasConfigProperty=((AtfconfiguratorUI) getUI()).getWasConfiguration();	
		setSpacing(true);
        setMargin(true);

        Label title = new Label(wasConfigProperty.getString("appName"));
        title.addStyleName("h2");
        title.addStyleName("colored");
        addComponent(title);
        
		final FormLayout form = new FormLayout();
        form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        addComponent(form);
        
        //CluserName
        final TextField cName = new TextField("Cluster Name");
        cName.setWidth("50%");
        cName.setValue(wasConfigProperty.getString("clusterName",""));
        cName.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("clusterName", value);
			}
        	
        });
        form.addComponent(cName);
        
       //JVM Suffice
        final TextField jvmNameSuffix = new TextField("JVM Name Suffix");
        jvmNameSuffix.setWidth("50%");
        jvmNameSuffix.setValue(wasConfigProperty.getString("jvmNameSuffix",""));
        jvmNameSuffix.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("jvmNameSuffix", value);
			}
        	
        });
        form.addComponent(jvmNameSuffix);
        //noOfClusters
        final TextField noOfClusters = new TextField("Number of Cluster Member");
        int noOfClustersValue=wasConfigProperty.getInt("noOfClusters");
        noOfClusters.setValue(String.valueOf(noOfClustersValue));
        noOfClusters.setWidth("50%");
        //nJDBC.setIcon(FontAwesome.PLUS);
        noOfClusters.addValueChangeListener(new Property.ValueChangeListener(){

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("noOfClusters",  (String) event.getProperty().getValue());
				((AtfconfiguratorUI) getUI()).navigator.navigateTo("Cluster");
			}        	
        });
        form.addComponent(noOfClusters);
        form.addComponent(new CollapseableBox("Member Nodes",getClusterMemberEntries(noOfClustersValue), false));
        
        //serverStartingPort
        final TextField serverStartingPort = new TextField("JVM Starting Port");
        serverStartingPort.setWidth("50%");
        serverStartingPort.setValue(wasConfigProperty.getString("serverStartingPort",""));
        serverStartingPort.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("serverStartingPort", value);
			}
        	
        });
        form.addComponent(serverStartingPort);
        
	}
	
	public FormLayout getClusterMemberEntries(int pos){
		final FormLayout form = new FormLayout();
		form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        for(int i=1; i<pos+1; i++){
        	
        	String member=wasConfigProperty.getString("clusterNode"+i+"Name", "");
        	final int currentPos= i;
        	final TextField mTextField = new TextField("Node");
        	mTextField.setValue(member);
        	mTextField.setWidth("100%");
        	mTextField.addValueChangeListener(new Property.ValueChangeListener(){

				@Override
				public void valueChange(ValueChangeEvent event) {
					// TODO Auto-generated method stub
					String value = (String) event.getProperty().getValue();
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("clusterNode"+currentPos+"Name", value);
				}
            	
            });
        	mTextField.setImmediate(true);
            form.addComponent(mTextField);
        }
		return form;
	}

}
